package com.vv.tools.modules.auth.handler;

/**
 * @author wen.li
 */

import com.vv.tools.common.message.Result;
import com.vv.tools.common.util.ResponseUtil;
import com.vv.tools.common.util.TokenUtil;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

/**
 * 自定义认证成功处理器
 */
@Component("loginSuccessHandler")
public class LoginSuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        UserDetails user = (UserDetails) authentication.getPrincipal();
        // 生成token
        String token = TokenUtil.genAccessToken(user.getUsername());
        ResponseUtil.of(response, Result.success(token));
    }

}
